<?php

namespace App\Http\Controllers\Serve;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class BaseServe
{
    public $locale;

    const data_validate_error = 2;
    public $req;

    public function __construct(Request $request)
    {
        $this->req = $this->req = $request;
        $this->locale = $this->req->getLocale();
    }

    /**
     * 数据验证， 如果数据验证出错， 则抛出错误
     * @param $data
     * @param $rules
     * @param $message
     * @throws \Exception
     */
    public function validate($data, $rules, $message)
    {
        $validator =Validator::make($data, $rules, $message);

        if ($validator->fails()){
            $message = $validator->errors()->first();
            throw new \Exception($message, self::data_validate_error);
        }

    }
}
